package ds_industry_2025.ds.Formal_volume2.T3

import org.apache.hudi.DataSourceWriteOptions.{PARTITIONPATH_FIELD, PRECOMBINE_FIELD, RECORDKEY_FIELD}
import org.apache.hudi.QuickstartUtils.getQuickstartWriteConfigs
import org.apache.spark.sql.SparkSession

/*
    2、根据dwd_ds_hudi层表统计每人每天下单的数量和下单的总金额，存入Hudi的dws_ds_hudi层的user_consumption_day_aggr表中
    （表结构如下），然后使用spark -shell按照客户主键、订单总金额均为降序排序，查询出前5条，将SQL语句复制粘贴至客户端桌面【
    Release\任务B提交结果.docx】中对应的任务序号下，将执行结果截图粘贴至客户端桌面【Release\任务B提交结果.docx】中对应的任务序
    号下；
 */
object t2 {
  def main(args: Array[String]): Unit = {
    val spark = SparkSession.builder()
      .master("local[*]")
      .appName("t2")
      .config("hive.exec.dynamic.partition.mode","nonstrict")
      .config("spark.serializer","org.apache.spark.serializer.KryoSerializer")
      .config("spark.sql.extensions","org.apache.spark.sql.hudi.HoodieSparkSessionExtension")
      .enableHiveSupport()
      .getOrCreate()

    spark.table("dwd.fact_order_info")
      .where("etl_date=(select max(etl_date) from dwd.fact_order_info)")
      .createOrReplaceTempView("order_info")

    spark.table("dwd.dim_user_info")
      .where("etl_date=(select max(etl_date) from dwd.dim_user_info)")
      .createOrReplaceTempView("user_info")

    val result = spark.sql(
      """
        |select
        |uuid() as uuid,
        |*
        |from(
        |select distinct
        |o.user_id,
        |u.name as user_name,
        |sum(o.final_total_amount)
        |over(partition by o.user_id,u.name,year(o.create_time),month(o.create_time),day(o.create_time)) as total_amount,
        |count(*) over(partition by o.user_id,u.name,year(o.create_time),month(o.create_time),day(o.create_time)) as total_count,
        |year(o.create_time) as year,
        |month(o.create_time) as month,
        |day(o.create_time) as day
        |from order_info as o
        |join user_info as u
        |on u.id=o.user_id
        |) as r1
        |""".stripMargin)

    // todo 建表语句
    spark.sql("create database if not exists dws_ds_hudi")
    spark.sql("use dws_ds_hudi")
    spark.sql(
      """
        |create table if not exists user_consumption_day_aggr(
        |uuid String,
        |user_id int,
        |user_name String,
        |total_amount double,
        |total_count int,
        |year int,
        |month int,
        |day int
        |)using hudi
        |tblproperties(
        |type="cow",
        |primaryKey="uuid",
        |preCombineField="total_count",
        |hoodie.datasource.hive_aync.mode="hms"
        |)
        |partitioned by(year,month,day)
        |""".stripMargin)

    result.write.format("hudi").mode("append")
      .options(getQuickstartWriteConfigs)
      .option(RECORDKEY_FIELD.key(),"uuid")
      .option(PRECOMBINE_FIELD.key(),"total_count")
      .option(PARTITIONPATH_FIELD.key(),"year,month,day")
      .option("hoodie.table.name","user_consumption_day_aggr")
      .save("hdfs://192.168.40.110:9000/user/hive/warehouse/dws_ds_hudi.db/user_consumption_day_aggr")

    spark.sql("select * from dws_ds_hudi.user_consumption_day_aggr order by user_id desc,total_amount desc limit 5").show


    spark.close()

  }

}
